<?php
/**
 *  學期上課日數
 * @author cik
 *
 */
class Default_Model_Upgrade_SemeCourseDate extends Default_Model_Upgrade_Core
{
    protected $_table = 'seme_course_date';
     protected $_targetTable = array('semester_course_days');


    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            $query = "SELECT * FROM seme_course_date ";
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            foreach($res as $row) {
                $arr= array();
                $year = (int)substr($row['seme_year_seme'],0,-1);
                $semester = (int)substr($row['seme_year_seme'],-1);
                $arr['year'] = $year;
                $arr['semester'] = $semester;
                $arr['grade'] = $row['class_year'];
                $find = $this->_newdbPdo->query("SELECT year FROM semester_course_days
				  WHERE year='{$arr['year']}'
				  AND semester='{$arr['semester']}'
				  AND grade='{$arr['grade']}'
				  ");
                if ($find->rowCount() > 0)
                continue;

                $arr['days'] = $row['days'];
                $query  = "INSERT INTO  semester_course_days (".implode(',',array_keys($arr)).") VALUES('".implode("','",array_values($arr))."')";
                $this->_newdbPdo->exec($query);
            }

        }


        //  $log['success_count'] = $success_count;
        // $log['error_count'] = $error_count;
        $log['memo'] = '學期上課日數設定';
        $log['upgrade_time']	= date("Y-m-d h:i:s");

        $this->finishUpgrade($log);

    }

}